package com.xs.gis.cover.util;

import com.xs.gis.cover.coverImpl.*;
import org.geotools.geometry.jts.JTS;
import org.geotools.referencing.CRS;
import org.geotools.styling.Style;
import org.locationtech.jts.geom.Geometry;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

/**
 * @program: cdgis_geotoolsplus
 * @description:
 * @author: w
 * @create: 2023-08-17 17:56
 **/
public class CoverFactory {

    /**
     * 坐标系转换
     *
     * @param geometry Geometry
     * @param sourceEpsg   源数据 参考系
     * @param targetEpsg   目标数据 参考系
     * @return 转换后的Geometry
     */
    public static Geometry crsCover(Geometry geometry, String sourceEpsg, String targetEpsg) {
        try {

            CRSAuthorityFactory factory = CRS.getAuthorityFactory(true);
            CoordinateReferenceSystem source = factory.createCoordinateReferenceSystem(sourceEpsg);
            CoordinateReferenceSystem target = factory.createCoordinateReferenceSystem(targetEpsg);
            MathTransform transform = CRS.findMathTransform(source, target,true);
            Geometry resultGeometry = JTS.transform(geometry, transform);
            return resultGeometry;
        } catch (FactoryException e) {
            throw new RuntimeException(e);
        } catch (org.opengis.referencing.operation.TransformException e) {
            throw new RuntimeException(e);
        }
    }



    /**
     * wkb转换器
     *
     * @return
     */
    public static WkbCover buildWkbCover() {
        return new WkbCover();
    }

    /**
     * wkb转换器
     *
     * @return
     */
    public static WkbCover buildWkbCover(String charSetName) {
        return new WkbCover(charSetName);
    }

    /**
     * wkt转换器
     *
     * @return
     */
    public static WktCover buildWktCover() {
        return new WktCover();
    }

    /**
     * GeoJson转换器
     *
     * @return
     */
    public static GeoJsonCover buildGeoJsonCover() {
        return new GeoJsonCover();
    }


    /**
     * shpFile转换器
     *
     * @return
     */
    public static ShpFileCover buildShpFileCover() {
        return new ShpFileCover();
    }

    public static ExcelCover buildExcelCover(){
        return new ExcelCover();
    }

    public static CsvCover buildCsvCover(){
        return new CsvCover();
    }


    /**
     * shpFile转换器
     *
     * @return
     */
    public static ShpFileCover buildShpFileCover(String charset) {
        return new ShpFileCover(charset);
    }


    /**
     * Gdb转换器
     *
     * @return
     */
    public static GdbCover buildGdbCover() {
        return new GdbCover();
    }

    /**
     * gdb转换器 指定坐标线
     * @return
     */
    public static GdbCover buildGdbCover(String epsgCode) {
        return new GdbCover(epsgCode);
    }

    /**
     * sqlite 转换器
     * @return
     */
    public static SqliteCover buildSqliteCover() {
        return new SqliteCover();
    }

    /**
     * postgis 转换器
     * @return
     */
    public static PostGISCover buildPostGISCover() {
        return new PostGISCover();
    }
    /**
     * postgis 转换器,指定图层名和坐标系
     * @return
     */
    public static PostGISCover buildPostGISCover(String layerName,String epsgCode) {
        return new PostGISCover(layerName,epsgCode);
    }
    /**
     * postgis 转换器,指定图层名
     * @return
     */
    public static PostGISCover buildPostGISCover(String layerName) {
        return new PostGISCover(layerName);
    }
    /**
     * postgis 图片转换器
     * @return
     */
    public static ImgCover buildImgCover( ) {
        return new ImgCover();
    }
    /**
     * @param imgPath 文件路径
     * @return
     */
    public static ImgCover buildImgCover(String imgPath) {
        return new ImgCover(imgPath);
    }
    /**
     *
     * @param imgPath 文件路径
     * @param stylePath 样式路径
     * @return
     */
    public static ImgCover buildImgCover(String imgPath, String stylePath ) {
        return new ImgCover(imgPath,stylePath);
    }

    /**
     * 文件转换
     * @return
     */
    public static GisTxtCover buildTextCover() {
        return new GisTxtCover();
    }
    /**
     * 文件转换
     * @return
     */
    public static GisTxtCover buildTextCover(String text) {
        return new GisTxtCover(text);
    }
    /**
     * 文件转换
     * @return
     */
    public static GisTxtCover buildTextCover(String text,String charSetName) {
        return new GisTxtCover(text,charSetName);
    }

    /**
     *
     * @param imgPath 文件路径
     * @param stylePath 样式路径
     * @param width 宽度
     * @param height 高度
     * @return
     */
    public static ImgCover buildImgCover(String imgPath, String stylePath, int width, int height, Style style) {
        return new ImgCover(imgPath,stylePath,width,height,style);
    }



}
